package com.shuke.sk_worm.tool.hiskpi.Impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.druid.util.StringUtils;
import com.shuke.sk_worm.consts.AbnormalTypeEnum;
import com.shuke.sk_worm.domain.SkAccountInfo;
import com.shuke.sk_worm.domain.SkRealData;
import com.shuke.sk_worm.domain.SkShopCookie;
import com.shuke.sk_worm.domain.SkShopInfo;
import com.shuke.sk_worm.service.Impl.SkShopCookieServiceImpl;
import com.shuke.sk_worm.service.SkShopInfoService;
import com.shuke.sk_worm.tool.Image;
import com.shuke.sk_worm.tool.Opencv;
import com.shuke.sk_worm.tool.TimeUtil;
import com.shuke.sk_worm.vo.RealDataVo;
import com.shuke.sk_worm.vo.ShopInfoListVo;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileOutputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;

@Component
public class DouyinShopImpl extends ShopDataImpl{

    //实时数据地址
    public String realDataUrl = "https://pigeon.jinritemai.com/backstage/getRealtimeStaffStatistics?page=1&size=1000&queryStaffName=%s";

    @Autowired
    private SkShopInfoService skShopInfoService;

    public Boolean updateShopRealUrlData(SkShopInfo shop, String cookie, List<SkAccountInfo> shopAccountList){
        List<String> shopAccounts = shopAccountList.stream().map(vo->{
            return vo.getShopAccount().trim();
        }).collect(Collectors.toList());

        String account = "";
        if (shopAccounts.size() == 1 && !StringUtils.isEmpty(shopAccountList.get(0).getUserName())){
            account = shopAccountList.get(0).getUserName();
        }
        String url = String.format(realDataUrl, account);

        Map<String, String> header = new HashMap<>();
        header.put("Cookie", cookie);
        HttpResponse response = HttpUtil.createGet(url).addHeaders(header).execute();
        logger.info(response.body());
        JSONObject jsonObject = JSONUtil.parseObj(response.body());
        Boolean status = false;
        if ("0".equals(String.valueOf(jsonObject.get("code")))) {

            List<RealDataVo> realDataVos = JSONUtil.toList(JSONUtil.parseArray(jsonObject.get("data")), RealDataVo.class);

            for(RealDataVo real : realDataVos){
                if (shopAccounts.contains(real.getStaff_username())) {
                    status = true;
                }
            }
            if (status){
                skShopInfoService.updateNotAbnormalByShopId(shop.getId());
            }else{
                if (StringUtils.isEmpty(shop.getErrorTime())){
                    skShopInfoService.updateAbnormalTime(shop.getId(), DateUtil.date());
                }else{
                    skShopInfoService.updateAbnormal(shop.getId());
                }

            }
        }
        return status;
    }
}
